﻿using System;
using System.IO;
using Microsoft.Reporting.WebForms;
using Monkey.Warrenty.Biz;

namespace MonkeyWarranty.Web.InstantTickets
{
    public partial class PrintTicket : System.Web.UI.Page
    {
        #region Properties

        private InstantTicketBiz _instantTicketBiz;
        private InstantTicketBiz InstantTicketManager
        {
            get { return _instantTicketBiz ?? (_instantTicketBiz = new InstantTicketBiz()); }
        }

        private long _ticketId;
        private long TicketId
        {
            get
            {
                if (_ticketId <= 0)
                    long.TryParse(Request.Params["id"], out _ticketId);
                return _ticketId;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (TicketId <= 0) return;

                FileStream report = null;
                FileStream subreport = null;

                try
                {
                    rvInstantTicket.ProcessingMode = ProcessingMode.Local;

                    // Load the report from a stream. 
                    report = new FileStream(Server.MapPath("~/Rdlcs/InstantTicket.rdlc"), FileMode.Open, FileAccess.Read);
                    rvInstantTicket.LocalReport.LoadReportDefinition(report);
                    
                    //// Load all the subreports contained in the report using LoadSubReportDefinition. The name of the subreport must match the one defined
                    //// in the main report stream (in the <Subreport>\<ReportName> XML element)
                    subreport = new FileStream(Server.MapPath("~/Rdlcs/InstantTicketDetails.rdlc"), FileMode.Open, FileAccess.Read);
                    rvInstantTicket.LocalReport.LoadSubreportDefinition("InstantTicketDetails", subreport);

                    var instantTicketForPrint = InstantTicketManager.GetInstantTicketForPrint(TicketId);
                    if (instantTicketForPrint != null)
                    {
                        // report data source
                        rvInstantTicket.LocalReport.DataSources.Add(
                            new ReportDataSource(rvInstantTicket.LocalReport.GetDataSourceNames()[0],
                                                 instantTicketForPrint));
                        rvInstantTicket.LocalReport.SubreportProcessing += LocalReport_SubreportProcessing;
                    }
                }
                finally
                {
                    if (report != null)
                        report.Dispose();
                    if (subreport != null)
                        subreport.Dispose();
                }
            }
        }

        private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
        {
            // subreport data source
            var ticketDetails = InstantTicketManager.GetInstantTicketTaskServiceDetails(TicketId);
            if (ticketDetails != null)
                e.DataSources.Add(new ReportDataSource(e.DataSourceNames[0], ticketDetails));
        }
    }
}